$(function(){
	var op = homePage();
	op.getMerchant();
	op.getOrderInfo();
	op.getAdvertisement();
	op.getCommentary();
	
	function homePage(){
		var rs = {};

		rs.getMerchant=function(){
			$.ajax({
		        url:"/o2o-merchant/sec/rest/merchant",
		        type:"get"
		    }).done(function(msg){
		        $("#userPortrait").attr("src",msg.portrait);
		    });
		}

		rs.getOrderInfo=function(){
		    $.ajax({
                url: "/o2o-merchant/sec/rest/orderInfo",
                type: "get",
            }).done(function (msg) {
            		var number = 0;
            		var money = 0;
            		var day1 = new Date();
					day1.setTime(day1.getTime()-24*60*60*1000);
					var s1 =day1.getDate();
                	msg.forEach((item,index)=>{
                        if(item.orderStatus=="WAITING"){
                        	number += 1;
                        }
                        let day = new Date(item.orderDate.replace(new RegExp("-", "g"), "/"));
                        day.setTime(day.getTime());
                        let s =day.getDate();
                        if(s-s1==0&&item.orderStatus=="COMPLETED"){
                        	 money += item.totalMoney;
                        }
                    })
                    $("#waitingNumber").text(number);
                    $("#money").text(money);
            });
		}

		rs.getAdvertisement=function(){
			$.ajax({
		        url:"/o2o-merchant/sec/rest/advertisement",
                type:"get"
		    }).done(function(msg){
		        var advertisementNumber = 0;
		        msg.forEach((item,index)=>{
                        if(item.advertisementStatus=="ON"){
                        	advertisementNumber += 1;
                        }
                    })
		        $("#advertisementNumber").text(advertisementNumber);
		    });
		}

		rs.getCommentary=function(){
			$.ajax({
		        url: "/o2o-merchant/sec/rest/commentary",
                type: "get",
		    }).done(function(msg){
		        var commentaryNumber = 0;
		        msg.forEach((item,index)=>{
                        if(item.merchantCommentary==""||item.merchantCommentary==null){
                        	commentaryNumber += 1;
                        }
                    })
		        $("#commentaryNumber").text(commentaryNumber);
		    });
		}

		return rs;
	}


 	
});